package com.zbkc.main;

import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.zbkc.bean.BeifenDirectoryBean;
import com.zbkc.bean.ShareFileBean;
import com.zbkc.interfaces.SCInterface;
import com.zbkc.main.base.BaseTitleActivity;
import com.zbkc.util.AlertUtils;
import com.zbkc.util.BASE64Util;
import com.zbkc.util.ThreadPoolFactory;

public class ShareDirectoryActivity extends BaseTitleActivity {

	private List<BeifenDirectoryBean> aList;
	private List<ShareFileBean> fileBeans;
	private String deviceNo;
	Handler handler = new Handler() {
		public void handleMessage(Message paramAnonymousMessage) {
			super.handleMessage(paramAnonymousMessage);
			switch (paramAnonymousMessage.what) {
			case 1:
//				if (aList.size() > 0) {
//					myAdapter = new MyAdapter(ShareDirectoryActivity.this, 0,
//							fileBeans);
//					listView.setAdapter(myAdapter);
//				} else {
//					// isLastDirectory = true;
//					// Intent localIntent = new Intent();
//					// localIntent.setClass(ShareDirectoryActivity.this,
//					// ShareFileActivity.class);
//					// localIntent.putExtra("id",
//					// ShareDirectoryActivity.this.deviceNo);
//					// startActivity(localIntent);
//				}
				myAdapter = new MyAdapter(ShareDirectoryActivity.this, 0,
						fileBeans);
				listView.setAdapter(myAdapter);
				break;
			}
			AlertUtils.dismissLoadingDialog();
		}

	};
	private boolean isLastDirectory = false;
	private ListView listView;
	private MyAdapter myAdapter;
	
	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		setContentView(R.layout.activity_share_directory);
		bindView();
		String str = BASE64Util.encode("-1".getBytes());
		getDirectory(str, str);
	}

	private void bindView() {
		setTitle("我的共享");
		setBtn2Visiable(false);
		deviceNo = getIntent().getStringExtra("id");
		listView = ((ListView) findViewById(R.id.listView));
		listView.setOnItemClickListener(new ItemClick());
	}

	private void getDirectory(final String paramString1,
			final String paramString2) {
		AlertUtils.showLoadingDialog(this, "正在获取数据,请稍后...", true);
		ThreadPoolFactory.getInstance().execute(new Runnable() {
			public void run() {
//				aList = SCInterface.getShareDictory(deviceNo, paramString1,
//						paramString2);
				fileBeans = SCInterface.getShareFile(deviceNo, paramString1, paramString2);
				handler.sendEmptyMessage(1);
			}
		});
	}

	

	class ItemClick implements AdapterView.OnItemClickListener {
		ItemClick() {
		}

		public void onItemClick(AdapterView<?> paramAdapterView,
				View paramView, int paramInt, long paramLong) {
			final ShareFileBean shareFileBean = fileBeans.get(paramInt);
			ThreadPoolFactory.getInstance().execute(new Runnable() {
				
				@Override
				public void run() {
					SCInterface.downFile(shareFileBean.getMake1(), shareFileBean.getMake2(), shareFileBean.getFilename());
				}
			});
//			if (!isLastDirectory) {
//				String str1 = aList.get(paramInt).getDirectoryMark1();
//				String str2 = aList.get(paramInt).getDirectoryMark2();
//				getDirectory(str1, str2);
//			}
			// Intent localIntent = new Intent();
			// localIntent.setClass(ShareDirectoryActivity.this,
			// ShareFileActivity.class);
			// localIntent.putExtra("id", ShareDirectoryActivity.this.deviceNo);
			// startActivity(localIntent);
		}
	}

	class MyAdapter extends ArrayAdapter<ShareFileBean> {
		public MyAdapter(Context context, int textViewResourceId,
				List<ShareFileBean> objects) {
			super(context, textViewResourceId, objects);
		}

		public View getView(int paramInt, View paramView,
				ViewGroup paramViewGroup) {
			ViewHolder viewHolder;
			if (paramView == null) {
				viewHolder = new ViewHolder();
				paramView = View.inflate(getContext(),
						R.layout.listview_beifen_item, null);
				viewHolder.tvtitle = ((TextView) paramView
						.findViewById(R.id.tv_title));
				paramView.setTag(viewHolder);
			} else {
				viewHolder = ((ViewHolder) paramView.getTag());
			}
			viewHolder.tvtitle
					.setText(((ShareFileBean) getItem(paramInt))
							.getFilename());
			return paramView;
		}

		class ViewHolder {
			TextView tvtitle;
		}
	}

}
